package vip.george.p1275;

import java.util.Arrays;
import java.util.HashMap;

public class P1275 {

    /**
     * 判断当新棋子落下时，是否获胜
     * @param map
     * @param index
     * @param player
     * @return
     */
    public Boolean checkWin(HashMap<Integer, String> map, int index, String player) {
        String exist1 = null;
        String exist2 = null;
        switch (index) {
            case 0:
                exist1 = map.get(1);
                exist2 = map.get(2);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(3);
                exist2 = map.get(6);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(4);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 1:
                exist1 = map.get(0);
                exist2 = map.get(2);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(4);
                exist2 = map.get(7);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 2:
                exist1 = map.get(0);
                exist2 = map.get(1);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(5);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(4);
                exist2 = map.get(6);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 3:
                exist1 = map.get(0);
                exist2 = map.get(6);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(4);
                exist2 = map.get(5);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 4:
                exist1 = map.get(3);
                exist2 = map.get(5);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(1);
                exist2 = map.get(7);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(0);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(2);
                exist2 = map.get(6);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 5:
                exist1 = map.get(3);
                exist2 = map.get(4);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(2);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 6:
                exist1 = map.get(0);
                exist2 = map.get(3);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(7);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(2);
                exist2 = map.get(4);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 7:
                exist1 = map.get(6);
                exist2 = map.get(8);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(1);
                exist2 = map.get(4);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
            case 8:
                exist1 = map.get(6);
                exist2 = map.get(7);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(0);
                exist2 = map.get(4);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                exist1 = map.get(2);
                exist2 = map.get(5);
                if (player.equals(exist1) && player.equals(exist2)) {
                    return true;
                }
                break;
        }
        return false;
    }

    /**
     * 方法1:
     * 创建一个哈希表，保存棋盘中的棋子
     * @param moves
     * @return
     */
    public String tictactoe(int[][] moves) {
        HashMap<Integer, String> map = new HashMap<>();
        for (int i = 0; i < moves.length; i++) {
            int[] move = moves[i];
            int x = move[0], y = move[1];
            int index = x * 3 + y;
            String player = i % 2 == 0 ? "A" : "B";
            // 只有大于等于5个数时才进行判断
            if (i >= 4 && checkWin(map, index, player)) {
                return player;
            } else {
                map.put(index, player);
            }
        }
        //遍历完所有的一维数组（即所有的操作）
        return moves.length == 9 ? "Draw" : "Pending";
    }

    public static void main(String[] args) {
        P1275 p1275 = new P1275();
        int[][] moves = new int[][]{{0, 0}, {2, 0}, {1, 1}, {2, 1}, {2, 2}};
        System.out.println(p1275.tictactoe(moves));
        System.out.println(p1275.tictactoe(new int[][]{{0, 0}, {1, 1}, {0, 1}, {0, 2}, {1, 0}, {2, 0}}));
        System.out.println(p1275.tictactoe(new int[][]{{0, 0}, {1, 1}, {2, 0}, {1, 0}, {1, 2}, {2, 1}, {0, 1}, {0, 2}, {2, 2}}));
    }
}
